package com.eboss2.sbs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.eboss2.sbs.R;
import com.eboss2.sbs.enums.EnumConsts;
import com.eboss2.sbs.interfaces.ExmineCallbackListener;
import com.eboss2.sbs.model.ExmineManageInfo;
import com.mytools.tool.utils.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

public class ServiceExmineManagerAdapter extends BaseAdapter {

	private Context context;
	private List<ExmineManageInfo> datas;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	private ExmineCallbackListener listener;

	public ServiceExmineManagerAdapter(Context context, List<ExmineManageInfo> datas, ExmineCallbackListener listener) {
		this.context = context;
		this.datas = datas;
		this.inflater = LayoutInflater.from(context);
		this.listener = listener;
	}

	@Override
	public int getCount() {
		if(datas!=null&&datas.size()>0)return datas.size();
		else return 0;
	}

	@Override
	public ExmineManageInfo getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		


		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_exmine, null);
			
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_status_name = (TextView) convertView.findViewById(R.id.tv_status_name);
			holder.tv_category = (TextView) convertView.findViewById(R.id.tv_category);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
			holder.tv_kc = (TextView) convertView.findViewById(R.id.tv_kc);
			holder.tv_xl = (TextView) convertView.findViewById(R.id.tv_xl);
			holder.tv_new_amt = (TextView) convertView.findViewById(R.id.tv_new_amt);
			holder.tv_old_amt = (TextView) convertView.findViewById(R.id.tv_old_amt);
			holder.tv_btn_3 = (SuperButton) convertView.findViewById(R.id.tv_btn_3);
			holder.tv_btn_2 = (SuperButton) convertView.findViewById(R.id.tv_btn_2);
			holder.tv_from_to = (TextView) convertView.findViewById(R.id.tv_from_to);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.tv_btn_2.setVisibility(View.GONE);


		final ExmineManageInfo data = datas.get(position);



		StringBuilder builder = new StringBuilder();
		builder.append(StringUtils.isBlank(data.getCategoryFirstName()) ? "" : data.getCategoryFirstName());
		builder.append(StringUtils.isBlank(data.getCategorySecondName()) ? "" : ">"+data.getCategorySecondName());
		holder.tv_category.setText(builder);
//		holder.tv_from_to.setText(StringUtils.isBlank(data.getMerchantName()) ? "" : data.getMerchantName());

		DisplayImageOptions options = new DisplayImageOptions.Builder()//
				.showImageForEmptyUri(R.drawable.ic_public_nophoto)
				.showImageOnFail(R.drawable.ic_public_nophoto)
				.imageScaleType(ImageScaleType.EXACTLY)
				.cacheInMemory(true)//
				.cacheOnDisk(true)//
				.bitmapConfig(android.graphics.Bitmap.Config.RGB_565)//
				.build();
		ImageLoader.getInstance().displayImage(data.getPicture(), holder.img, options);

		holder.tv_content.setText(data.getName());
//		holder.tv_new_amt.setText("￥"+StringUtils.formatIntMoney(data.getNoVipPrice()));
//		holder.tv_old_amt.setText("￥"+StringUtils.formatIntMoney(data.getSalePrice()));
		holder.tv_kc.setText("库存: "+data.getStockNumber());
		holder.tv_xl.setText("销量: "+data.getSaleNum());
		holder.tv_new_amt.setText("￥"+StringUtils.formatIntMoney(data.getPromotionPrice()));
		holder.tv_old_amt.setText("现价: ￥"+StringUtils.formatIntMoney(data.getNoVipPrice()));


		if (EnumConsts.ServiceExmineStatus.EXMINE_STATUS_DSH.getState() == data.getAuditStatus()){ //待审核
			holder.tv_status_name.setText(EnumConsts.ServiceExmineStatus.getByState(data.getAuditStatus()).getName());
			holder.tv_status_name.setTextColor(context.getResources().getColor(EnumConsts.ServiceExmineStatus.getByState(data.getAuditStatus()).getColor()));
			holder.tv_time.setText("提交审核时间: "+data.getGmtModified());
			holder.tv_btn_2.setVisibility(View.GONE);
		}else if (EnumConsts.ServiceExmineStatus.EXMINE_STATUS_SHJJ.getState() == data.getAuditStatus()){ // 审核拒绝
			holder.tv_status_name.setText(EnumConsts.ServiceExmineStatus.getByState(data.getAuditStatus()).getName());
			holder.tv_status_name.setTextColor(context.getResources().getColor(EnumConsts.ServiceExmineStatus.getByState(data.getAuditStatus()).getColor()));
			holder.tv_time.setText("审核拒绝时间: "+data.getGmtModified());
			holder.tv_btn_2.setVisibility(View.VISIBLE);
		}else if (EnumConsts.ServiceExmineStatus.EXMINE_STATUS_SHTG.getState() == data.getAuditStatus()){ //审核通过
			holder.tv_status_name.setText(EnumConsts.ServiceExmineStatus.getByState(data.getAuditStatus()).getName());
			holder.tv_status_name.setTextColor(context.getResources().getColor(EnumConsts.ServiceExmineStatus.getByState(data.getAuditStatus()).getColor()));
			holder.tv_time.setText("审核通过时间: "+data.getGmtModified());
			holder.tv_btn_2.setVisibility(View.GONE);
		}

		holder.tv_btn_2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (listener != null){
					listener.commit(data.getId());
				}
			}
		});

		holder.tv_btn_3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (listener != null){
					listener.checkDetail(data.getId());
				}
			}
		});



		return convertView;
	}





	public final class ViewHolder {
		private TextView tv_status_name;
		private TextView tv_time;
		private TextView tv_category;
		private ImageView img;
		private TextView tv_content;
		private TextView tv_kc;
		private TextView tv_xl;
		private TextView tv_new_amt;
		private TextView tv_old_amt;
		private SuperButton tv_btn_2;
		private SuperButton tv_btn_3;
		private TextView tv_from_to;
	}

}
